package com.top.dao;

import com.top.entiy.Food;
import com.top.entiy.FoodCategory;
import com.top.entiy.Order;
import com.top.entiy.OrderItem;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author:
 * @DateTime: 2023/12/10 09:09
 * @Description: TODO
 */
public class OrderItemDao extends AbstractDAO implements IDAO<Integer, OrderItem> {

    @Override
    public boolean doCreate(OrderItem vo) {
        String sql = "INSERT INTO `ordering_food`.`order_item` " +
                "(`order_id`, `food_id`, `num`, `price`, `amount`,create_time,update_time) " +
                "VALUES ( ?, ?, ?, ?, ?,now(),now())";
        try {
            statement = conn.prepareStatement(sql);
            statement.setInt(1, vo.getOrderId());
            statement.setInt(2, vo.getFoodId());
            statement.setInt(3, vo.getNum());
            statement.setBigDecimal(4, vo.getPrice());
            statement.setBigDecimal(5, vo.getAmount());
            return super.statement.executeUpdate() > 0;
        } catch (SQLException e) {
            System.err.println("创建失败" + e);
        }
        return false;
    }

    @Override
    public boolean doUpdate(OrderItem vo) throws SQLException {
        return false;
    }

    @Override
    public boolean doRemove(Integer id) throws SQLException {
        return false;
    }

    @Override
    public OrderItem findById(Integer id) {
        return null;
    }

    public List<OrderItem> findAllByOrderId(Integer orderId) {
        String sql = "SELECT oi.*,f.`name` FROM `ordering_food`.`order_item` oi\n" +
                "LEFT JOIN food f on f.id = oi.food_id\n" +
                " WHERE `order_id` =?";
        List<OrderItem> all = new ArrayList<>();
        try {
            statement = conn.prepareStatement(sql);
            statement.setInt(1, orderId);

            ResultSet rs = statement.executeQuery();
            while (rs.next()) {
                OrderItem orderItem = new OrderItem();
                orderItem.setId(rs.getInt("id"));
                orderItem.setOrderId(rs.getInt("order_id"));
                orderItem.setFoodId(rs.getInt("food_id"));
                orderItem.setNum(rs.getInt("num"));
                orderItem.setFoodName(rs.getString("name"));
                orderItem.setPrice(rs.getBigDecimal("price"));
                orderItem.setAmount(rs.getBigDecimal("amount"));
                all.add(orderItem);
            }
        } catch (SQLException e) {
            System.err.println("查询失败" + e);
        }
        return all;
    }

    @Override
    public List<OrderItem> findAll() throws SQLException {
        return null;
    }

    @Override
    public List<OrderItem> findAllBySplit(String column, String keyWord, Integer currentPage, Integer lineSize) throws SQLException {
        return null;
    }

    @Override
    public Integer getAllCount(String column, String keyWord) throws SQLException {
        return null;
    }
}
